#pragma once
#include "mysql_connection/include/jdbc/cppconn/driver.h"
#include "mysql_connection/include/jdbc/cppconn/connection.h"
#include "mysql_connection/include/jdbc/cppconn/statement.h"
#include "mysql_connection/include/jdbc/cppconn/resultset.h"
#include <iostream>
#include <memory>
#include <string>

class MySQL
{
public:
    MySQL()
        :_driver(get_driver_instance())
        ,_conn(_driver->connect("tcp://113.45.246.148:3306/conn", "wgm", "wen2024")) // 添加数据库名
        ,_stmt(_conn->createStatement())
    {
        // 设置字符集
        _stmt->execute("SET NAMES utf8mb4");
    }

    std::shared_ptr<sql::ResultSet> ExcuteQuery(const std::string& sql)
    {
        return std::shared_ptr<sql::ResultSet>(_stmt->executeQuery(sql));
    }

    int ExecuteUpdate(const std::string& sql)
    {
        return _stmt->executeUpdate(sql);
    }

    // 插入数据
    bool InsertUser(int session, const std::string& name, const std::string& password)
    {
        try {
            std::string sql = "INSERT INTO user (session, name, password) VALUES (" +
                             std::to_string(session) + ", '" + name + "', '" + password + "')";
            int affected_rows = ExecuteUpdate(sql);
            return affected_rows > 0;
        }
        catch (sql::SQLException& e) {
            std::cerr << "插入错误: " << e.what() << std::endl;
            return false;
        }
    }

    // 查询所有用户
    void QueryAllUsers()
    {
        try {
            std::shared_ptr<sql::ResultSet> res = ExcuteQuery("SELECT * FROM user");
            std::cout << "\n所有用户数据:" << std::endl;
            std::cout << "session\tname\tpassword" << std::endl;
            std::cout << "----------------------------" << std::endl;
            while (res->next()) {
                std::cout << res->getInt("session") << "\t"
                          << res->getString("name") << "\t"
                          << res->getString("password") << std::endl;
            }
        }
        catch (sql::SQLException& e) {
            std::cerr << "查询错误: " << e.what() << std::endl;
        }
    }

    // 查询特定名字的用户
    void QueryUserByName(const std::string& name)
    {
        try {
            std::string sql = "SELECT * FROM user WHERE name = '" + name + "'";
            std::shared_ptr<sql::ResultSet> res = ExcuteQuery(sql);
            
            std::cout << "\n查询用户 '" << name << "' 的结果:" << std::endl;
            if (res->next()) {
                std::cout << "session: " << res->getInt("session") << std::endl;
                std::cout << "name: " << res->getString("name") << std::endl;
                std::cout << "password: " << res->getString("password") << std::endl;
            } else {
                std::cout << "未找到用户: " << name << std::endl;
            }
        }
        catch (sql::SQLException& e) {
            std::cerr << "查询错误: " << e.what() << std::endl;
        }
    }

    ~MySQL()
    {
        _conn->close();
        _stmt->close();
    }

private:
    sql::Driver* _driver;
    std::unique_ptr<sql::Connection> _conn;
    std::unique_ptr<sql::Statement> _stmt;
};
